
import path = require("path")
import fs = require("fs")
import express = require("express");
import { AccountReqHandler } from "./AccountReqHandler";
import { ServerConfig } from "../src/config/ServerConfig";
import { Logger } from "../src/engine/utils/Logger";
import { DbConfig } from "../src/config/DbConfig";
import { Config } from "../src/config/Config";
import { TimerUtil } from "../src/engine/utils/TimerUtil";
import { ServerManager } from "../src/server/ServerManager";
import { LogTest } from "../log_server/LogTest";
import { DBSourceManager } from "../src/engine/utils/DBSourceManager";
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser')
const http = require("http");

/**
 */
export class account_server {

    public static instance: account_server = new account_server()


    public async main() {
        let port: number = parseInt(process.argv[2]);
        if (isNaN(port)) {
            Logger.error("port为空")
            return;
        }

        let environment: number = parseInt(process.argv[3]);
        if (isNaN(port)) {
            Logger.warn("environment为空")
            return;
        }
        Config.environment = environment;
        if (Config.environment == Config.EnvironmentType.Online) {
            // Logger.LEVEL = Logger.LOG_LEVEL_TYPES.WARN;
            Logger.LEVEL = Logger.LOG_LEVEL_TYPES.DEBUG;
        } else {
            Logger.LEVEL = Logger.LOG_LEVEL_TYPES.DEBUG
        }
        Config.serverType = Config.ServerType_List.account_server;
        Logger.tag = Config.serverType + "-" + port
        DbConfig.init(environment)
        DBSourceManager.instance.init();
        TimerUtil.init();
        await ServerConfig.initConfig(port)


        let app = express();
        let publicPath = path.resolve() + "/public";
        if (!fs.existsSync(publicPath)) {
            fs.mkdirSync(publicPath);
        }
        app.use(bodyParser.urlencoded({ extended: false })); //解析post请求数据
        app.use(bodyParser.text()); // for parsing application/json
        app.use(bodyParser.json()); // for parsing application/json

        //设置静态网页目录
        let options = {
            setHeaders: function (res, path, stat) {
                res.set('Access-Control-Allow-Origin', '*')
            }
        }
        app.use(express.static(publicPath, options));
        app.use(cookieParser())
        app.all('*', function (req, res, next) {
            req.header("Content-Type", "application/json;charset=UTF-8");
            res.header("Access-Control-Allow-Origin", "*");//项目上线后改成页面的地址
            res.header("Content-Type", "application/json;charset=UTF-8");
            res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
            next();
        });
        AccountReqHandler.instance.init(app)
        http.createServer(app).listen(port);
        // ServerManager.instance.initServerSockets()
        Logger.info("start suc=", ServerConfig.nowServerConfig);
        LogTest.instance.join()
    }
}

try {
    account_server.instance.main()
} catch (error) {
    Logger.info("error=", error)
    Logger.error("error=", error)
}